package com.szholly.utils.spring.springmvc.route;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.utils.util.StringUtils;

@Controller
public class RouteController {

	/**
	 * ���幦�ܼ�
	 */
	public static final HashMap<String, FunctionInfo> Routes = new HashMap<String, FunctionInfo>();

	/**
	 * ע�Ṧ��
	 * 
	 * @param info
	 */
	public static void regFunction(FunctionInfo info) {
		if (info == null) {
			return;
		}
		String key = info.getRouteName();
		if (Routes.containsKey(key)) {
			Routes.remove(key);
		}
		Routes.put(key, info);
	}

	/**
	 * ��ע�Ṧ��
	 * 
	 * @param info
	 */
	public static void unRegFunction(FunctionInfo info) {
		if (info == null) {
			return;
		}
		String key = info.getRouteName();
		if (Routes.containsKey(key)) {
			Routes.remove(key);
		}
	}

	/**
	 * ����ͨ��·��
	 * 
	 * @param req
	 * @param model
	 * @param url
	 * @return
	 */
	@RequestMapping(value = "/route")
	public String route(HttpServletRequest req, Model model, String url) {
		if (StringUtils.IsNullOrEmpty(url)) {
			return url;
		}
		if (Routes.containsKey(url)) {
			return Routes.get(url).getFunctionUrl();
		}
		return url;
	}
}
